package org.lucky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.lucky.mapper.LuckyMapper;
import org.lucky.mapper.UserMapper;
import org.lucky.service.IUserService;
import org.lucky_common.domain.LuckyShare;
import org.lucky_common.domain.User;
import org.lucky_common.util.AjaxResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService{

    @Resource
    private UserMapper userMapper;

    @Resource
    private LuckyMapper luckyMapper;

    @Override
    public AjaxResult saveWithCode(User user) {
        // 先检查用户是否已经存在，如果存在，是更新操作
        User existingUser = userMapper.selectOne(new QueryWrapper<User>().eq("inviteCode",user.getInviteCode()));
        if (existingUser != null){
            if ((existingUser.getEmail().equals(user.getEmail()) ||
                    userMapper.selectCount(new QueryWrapper<User>().eq("email", user.getEmail())) == 0) &&
                    (existingUser.getMobile().equals(user.getMobile()) ||
                            userMapper.selectCount(new QueryWrapper<User>().eq("mobile", user.getMobile())) == 0)) {
                existingUser.setUname(user.getUname());
                existingUser.setProvince(user.getProvince());
                existingUser.setCity(user.getCity());
                existingUser.setAddress(user.getAddress());
                existingUser.setEmail(user.getEmail());
                existingUser.setMobile(user.getMobile());
                int result = userMapper.updateById(existingUser); // 更新用户信息
                if (result > 0){
                    LuckyShare inviteCode = luckyMapper.selectOne(new QueryWrapper<LuckyShare>().eq("inviteCode", existingUser.getInviteCode()));
                    inviteCode.setSubmitParam(true);
                    luckyMapper.updateById(inviteCode);
                    return AjaxResult.success("用户信息更新成功");
                }else {
                    return AjaxResult.error("网络异常！请稍后再试！",1004);
                }
            } else {
                if (!existingUser.getEmail().equals(user.getEmail())) {
                    AjaxResult.error("邮箱已被使用", 1001);
                }
                if (!existingUser.getMobile().equals(user.getMobile())) {
                    AjaxResult.error("手机号已被使用",1002);
                }
                return AjaxResult.error("用户信息已存在",1003);
            }
        }else {
            LuckyShare inviteCode = luckyMapper.selectOne(new QueryWrapper<LuckyShare>().eq("inviteCode", user.getInviteCode()));
            if (inviteCode != null){
                boolean emailExists = userMapper.selectCount(new QueryWrapper<User>().eq("email", user.getEmail())) > 0;
                if (emailExists) {
                    // 处理邮箱已存在的情况，例如返回错误信息
                    return AjaxResult.error("邮箱已被使用", 1001);
                }

                boolean mobileExists = userMapper.selectCount(new QueryWrapper<User>().eq("mobile", user.getMobile())) > 0;
                if (mobileExists) {
                    // 处理手机号已存在的情况，例如返回错误信息
                    return AjaxResult.error("手机号已被使用",1002);
                }
                int result = userMapper.insert(user);
                if(result > 0) {
                    LuckyShare inviteCode2 = luckyMapper.selectOne(new QueryWrapper<LuckyShare>().eq("inviteCode", user.getInviteCode()));
                    inviteCode2.setSubmitParam(true);
                    luckyMapper.updateById(inviteCode2);
                    return AjaxResult.success("提交成功");
                } else {
                    return AjaxResult.error("提交失败",1004);
                }
            } else {
                return AjaxResult.error("邀请码已过期",1005);
            }
        }
    }
}
